package com.farm.service.impl;

import com.farm.dao.AccountDao;
import com.farm.domain.Account;
import com.farm.domain.Address;
import com.farm.service.AccountService;
import com.farm.utils.JwtUtil;
import com.farm.utils.Result;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@AllArgsConstructor
public class AccountServiceImpl implements AccountService {

    AccountDao accountDao;

    @Override
    public Account login(String loginName, String passWord) {
        return accountDao.login(loginName,passWord);
    }

    @Override
    public Result register(String name, String loginName, String password, String phone,String identity) {

        Account account = new Account();
        account.setName(name);
        account.setLoginName(loginName);
        account.setPassWord(password);
        account.setPhone(phone);
        account.setIdentity(identity);

        Integer result = accountDao.register(account);
        Result res = new Result();
        if (result==1){
            String token = JwtUtil.createToken(account);
            HashMap map = new HashMap();
            map.put("token",token);
            map.put("img","/assest/img/userImg/defaultHeader.png");
            map.put("nickname",name);
            map.put("loginName",loginName);
            map.put("phoneNum",phone);
            res.setCode(200);
            res.setData(map);
            return res;
        }
        res.setCode(400);
        return res;


//        return loginDao.register(user);
    }

    public Account repeat(String loginName){
        return accountDao.repeat(loginName);
    }

    @Override
    public Account checkToken(String nickname, String loginName) {
        Account account = new Account();
        account.setName(nickname);
        account.setLoginName(loginName);
        return accountDao.checkToken(account);
    }

    @Override
    public Integer uploadProfilePicture(Account account) {
        return accountDao.uploadProfilePicture(account);
    }

    @Override
    public Account getUserInfo(String loginName, String nickname) {
        Account account = new Account();
        account.setLoginName(loginName);
        account.setName(nickname);
        return accountDao.getUserInfo(account);
    }

    @Override
    public Integer addAddress(String loginName,Map params) {
        Address addressInfo = new Address();

        try{
            Account accountInfo = accountDao.getAll(loginName);
            String userId = accountInfo.getId();

            String province = (String) params.get("province");
            String city = (String) params.get("city");
            String area = (String) params.get("area");
            String addressDetail = (String) params.get("addressDetail");
            String phoneNum = (String) params.get("phoneNum");
            addressInfo.setUserID(userId);
            addressInfo.setProvince(province);
            addressInfo.setCity(city);
            addressInfo.setArea(area);
            addressInfo.setAddressDetail(addressDetail);
            addressInfo.setPhoneNum(phoneNum);
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
            addressInfo.setCollectionDate(simpleDateFormat.format(date));

            Integer result = accountDao.addAddress(addressInfo);
            if (result==1){
                return 1;
            }else {
                return 0;
            }

        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Integer updateAddress(Map params) {
        Address address = new Address();
        String id = (String) params.get("addressId");
        String province = (String) params.get("province");
        String city = (String) params.get("city");
        String area = (String) params.get("area");
        String addressDetail = (String) params.get("addressDetail");
        String phoneNum = (String) params.get("phoneNum");

        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

        address.setId(id);
        address.setProvince(province);
        address.setCity(city);
        address.setArea(area);
        address.setAddressDetail(addressDetail);
        address.setPhoneNum(phoneNum);
        address.setCollectionDate(format.format(new Date()));

        Integer res = accountDao.updateAddress(address);
        if (res!=0){
            return res;
        }else {
            return 0;
        }
    }

    @Override
    public Integer delAddressInfo(Map params) {
        String id = (String) params.get("addressId");
        if (id!=null&&!id.equals("")){
            Integer res = accountDao.delAddressInfo(id);
            return res;
        }else{
            return 0;
        }
    }

    @Override
    public List<Address> getAddressInfo(String loginName) {
        List<Address> list = null;

        Account accountInfo = accountDao.getAll(loginName);
        if (accountInfo !=null){
            String userID = accountInfo.getId();
             list = accountDao.getAddressInfo(userID);
             if (list!=null){
                 return list;
             }else{
                 return null;
             }
        }else{
            return null;
        }

    }

    @Override
    public Account getAll(String loginName){
       return accountDao.getAll(loginName);
    }

    @Override
    public Account getUserInfoById(String id) {
        return accountDao.getUserInfoById(id);
    }


    @Override
    public Integer userRemoval(String name, String loginName) {
        Account account = new Account();
        account.setName(name);
        account.setLoginName(loginName);
        Integer res = accountDao.userRemoval(account);
        if (res!=0){
            Account info = accountDao.getAll(loginName);
            if (info!=null&&info.getIdentity().equals("2")){
                accountDao.userRemovalProduceStatus(info.getId());
            }else if (info!=null&&info.getIdentity().equals("1")){
                return res;
            }else {
                return 0;
            }
        }else{
            return 0;
        }
        return  res;
    }

    @Override
    public Integer updateInfo(String loginName,String name, String password, String phone) {
        Account account = new Account();
        account.setLoginName(loginName);
        account.setName(name);
        account.setPassWord(password);
        account.setPhone(phone);
        return accountDao.updateInfo(account) ;
    }


}
